<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Paypal extends ADM_Controller {

    private $dataArray;

    function __construct() {
        parent::__construct();
        // Load helpers
        //$this->load->helper('url');
        // Load PayPal library
        //$this->config->load('paypal');

        $this->load->config('paypal', TRUE);
        $this->dataArray = $this->config->item('paypal');
        $this->load->library('paypal/Paypal_adaptive', $this->dataArray);
        $this->load->library('permissions');
        $this->load->model('pagamento_model');
        $this->checar_metodo(uri_string());
    }

    function pagar($dados=null) {
        $data = array();
        $b_consultar = $this->pagamento_model->get_categoria('paypal');
        if ($b_consultar != null) {
            foreach ($b_consultar as $value) {
                if ($value->tipo == 'bool')
                    $data[$value->nome] = (boolean) $value->valor;
                else
                    $data[$value->nome] = $value->valor;
            }
            $this->dataArray['Sandbox'] = isset($data['Sandbox']) ? $data['Sandbox'] : $this->dataArray['Sandbox'];
            $this->dataArray['APIUsername'] = isset($data['APIUsername']) ? $data['APIUsername'] : $this->dataArray['APIUsername'];
            $this->dataArray['APIPassword'] = isset($data['APIPassword']) ? $data['APIPassword'] : $this->dataArray['APIPassword'];
            $this->dataArray['APISignature'] = isset($data['APISignature']) ? $data['APISignature'] : $this->dataArray['APISignature'];
            $this->dataArray['DeviceID'] = isset($data['DeviceID']) ? $data['DeviceID'] : $this->dataArray['DeviceID'];
            $this->dataArray['ApplicationID'] = isset($data['ApplicationID']) ? $data['ApplicationID'] : $this->dataArray['ApplicationID'];
            $this->dataArray['DeveloperEmailAccount'] = isset($data['DeveloperEmailAccount']) ? $data['DeveloperEmailAccount'] : $this->dataArray['DeveloperEmailAccount'];

            //pre($data);

            $this->dataArray['PayRequestFields'] = array();
            //ActionType: 
            //PAY – Use this option if you are not using the Pay request in combination with ExecutePayment.
            //CREATE – Use this option to set up the payment instructions with SetPaymentOptions and then execute the payment at a later time with the ExecutePayment.
            //PAY_PRIMARY – For chained payments only, specify this value to delay payments to the secondary receivers; only the payment to the primary receiver is processed.
            $this->dataArray['PayRequestFields']['ActionType'] = 'PAY';
            //cancelUrl: é a página que será redirecionada se a compra não for aprovada
            $this->dataArray['PayRequestFields']['CancelURL'] = isset($data['CancelURL']) ? $data['CancelURL'] : '';
            //CurrencyCode: são os códigos com 3 caractéres dos dinheiros, lista de países: http://goo.gl/yVhTV
            $this->dataArray['PayRequestFields']['CurrencyCode'] = isset($data['CurrencyCode']) ? $data['CurrencyCode'] : '';
            //ReturnURL: URL para o Paypal retornar para a pagina q chamou
            $this->dataArray['PayRequestFields']['ReturnURL'] = isset($data['ReturnURL']) ? $data['ReturnURL'] : '';

            //dados do(s) recebedor(es)
            $this->dataArray['Receivers'] = array();
            $this->dataArray['Receivers'][0]['Amount'] = isset($dados['valor']) ? $dados['valor'] : '';//'100';
            $this->dataArray['Receivers'][0]['Email'] = isset($dados['email']) ? $dados['email'] : '';//'desenv_1334415176_biz@linknacional.com';
            $this->dataArray['Receivers'][0]['InvoiceID'] = isset($dados['invoice']) ? $dados['invoice'] : '';//'100';

            //PaymentType: GOOD, PERSONAL e SERVICE
            $this->dataArray['Receivers'][0]['PaymentType'] = 'PERSONAL';
            //PaymentSubType: AffiliatePayments, B2B, ChildCare, Donations, Entertainment, EventPlanning, GeneralContractors
            //Government, Insurance, Invoice, Medical, Mortgage, Payroll, Rebates, Refunds, Reimbursements, Remittances
            //Rent, Tourism, Transfer, Tuition, Utilities
            $this->dataArray['Receivers'][0]['PaymentSubType'] = '';
            $this->dataArray['Receivers'][0]['Phone'] = array();
            $this->dataArray['Receivers'][0]['Phone']['CountryCode'] = '';
            $this->dataArray['Receivers'][0]['Phone']['PhoneNumber'] = '';
            $this->dataArray['Receivers'][0]['Phone']['Extension'] = '';
            $this->dataArray['Receivers'][0]['Primary'] = 'TRUE';
        }
        //unset($retorno['Errors']);
        $retorno = $this->paypal_adaptive->pay($this->dataArray);
        pre($retorno);
        return json_encode($retorno);
    }

}

/* End of file adaptive_payments.php */
/* Location: ./system/application/controllers/paypal/adaptive_payments.php */